<template>
  <BoUploadExcel
    @download-template="onDownloadTemplate"
    :upload-api="importData"
    :columns="columns"
    title="台网台站批量导入"
    :submit-api="importBatch"
    @closed="onClose"
    accept=".xlsx, .xls"
  />
</template>

<script setup lang="tsx">
import { computed } from 'vue';
import { download } from '@boeyo/shared';
import { BoTableProps } from '@boeyo/ui';
import { BoUploadExcel } from '@boeyo/ui';
import { importBatch, importData, importTemplate } from '../api';

const emit = defineEmits<{
  importAfter: [];
}>();
const onDownloadTemplate = async () => {
  const res = await importTemplate();

  download(res.data as unknown as Blob, '台站台网导入模板.xlsx');
};

const onClose = () => {
  emit('importAfter');
};

const columns = computed<BoTableProps['columns']>(() => [
  {
    title: '序号',
    dataIndex: 'index',
    customRender({ renderIndex, index }) {
      return renderIndex + 1;
    },
    hideInSearch: true,
  },
  {
    title: '台网代码',
    dataIndex: 'networkCode',
    hideInSearch: true,
  },
  {
    title: '台站代码',
    dataIndex: 'stationCode',
    hideInSearch: true,
  },
  {
    title: '台站名称',
    dataIndex: 'stationName',
    hideInSearch: true,
  },
  {
    title: '经度',
    dataIndex: 'longitude',
    hideInSearch: true,
  },
  {
    title: '纬度',
    dataIndex: 'latitude',
    hideInSearch: true,
  },
  {
    title: '高程(m)',
    dataIndex: 'altitude',
    hideInSearch: true,
  },
]);
</script>
